package Interview.pinduoduo;

import com.google.common.collect.Lists;

import java.util.List;

/**
 * 递增数组中两个数的和等于sum
 *
 * @author leiwenkang
 * @date 2020/4/5
 */
public class Test03递增数组中两个数的和等于sum {
    public static void main(String[] args) {
        Test03递增数组中两个数的和等于sum test03递增数组中两个数的和等于sum = new Test03递增数组中两个数的和等于sum();
        int[] array = {1, 3, 4, 7, 8, 9, 10};
        List<Integer> list = test03递增数组中两个数的和等于sum.findSum(array, 12);
        System.out.println(list);
    }

    public List<Integer> findSum(int[] array, int sum) {
        if (array == null || array.length < 2) {
            return null;
        }

        List<Integer> result = Lists.newArrayList();

        int start = 0;
        int end = array.length - 1;
        while (start < end) {
            if (array[start] + array[end] == sum) {
                result.add(array[start]);
                result.add(array[end]);
                return result;
            } else if (array[start] + array[end] > sum) {
                end--;
            } else {
                start++;
            }
        }
        return result;
    }
}
